<?php

namespace Home\Controller;

class StoreController extends BaseController {

    private $model = null;

    function __construct() {
        parent::__construct();
        $this->model = D('Store');
    }

    function storeList() {
        $where = array('user_id' => parent::$user_id);
        $result = $this->model->getList($where);
        $this->assign('data', $result);
        $this->display();
    }

    function bindStore() {
        if (IS_POST) {
            $data = array(
                'user_id' => parent::$user_id,
                'name' => I('post.name', '', 'trim'),
                'remark' => I('post.remark', '', 'trim'),
                'status' => 1,
                'add_time' => time()
            );
            $result = $this->model->insert($data);
            if ($result > 0) {
                $this->success('绑定成功！');
            } else {
                $this->error('绑定失败！');
            }
        } else {
            $this->display();
        }
    }

    function modifyStore() {
        if (IS_POST) {
            $id = I('post.id', 0, 'intval');
            $data = array(
                'name' => I('post.name', '', 'trim'),
                'remark' => I('post.remark', '', 'trim')
            );
            $where = array(
                'id' => $id,
                'user_id' => parent::$user_id
            );
            $result = $this->model->modify($data, $where);
            if ($result !== false) {
                $this->success('修改成功！');
            } else {
                $this->error('修改失败！');
            }
        } else {
            $id = I('get.id', '', 'intval');
            $info = $this->model->getInfo($id);
            $this->assign('info', $info);
            $this->display('bindStore');
        }
    }

    function delStore() {
        $id = I('post.id', 0, 'intval');
        $result = $this->model->del($id);
        if ($result) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败！');
        }
    }

}
